#ifndef PLOG_FILE_APPENDER_H
#define PLOG_FILE_APPENDER_H

#include "appender.h"
#include <atlfile.h>

namespace plog
{

//////////////////////////////////////////////////////////////////////////
class atl_file_wrapper
{
public:
    explicit atl_file_wrapper(const wchar_t* file_name = NULL);
    bool open(const wchar_t* file_name);
    void write(const void*  pBuffer, size_t BufferLength);
    bool is_ready();
    ~atl_file_wrapper();
private:
    ATL::CAtlFile file_impl_;
};

template<typename file_wrapper>
class base_file_appender : public appender
{
public:
    base_file_appender(log_level min_severity = none)
        : appender(min_severity)
    {}

    base_file_appender(const wchar_t* file_name, log_level min_severity = none)
        : appender(min_severity)
    {
        this->init(file_name);
    }

    virtual ~base_file_appender(){}

    void init(const wchar_t* file_name)
    {
        bool success = file_.open(file_name);

        if( !success )
        {
            ATLTRACE("Failed to open log file");
        }
    }

    virtual void write(const void* buffer, size_t length)
    {
        if(file_.is_ready())
        {
            file_.write(buffer, length);
        }
    }

private:
    file_wrapper file_;
};

typedef base_file_appender<atl_file_wrapper> file_appender;

} //namespace

#endif